package com.zqcy.workbench.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Toast;

import com.bambooclod.eaccount3.call.EpassAuthSDK;
import com.bambooclod.eaccount3.callback.AuthCallBack;
import com.bambooclod.eaccount3.callback.SendSmsCallBack;
import com.shinemo.minisdk.util.ColorUtils;
import com.zqcy.workbench.R;
import com.zqcy.workbench.databinding.ActivityLoginBinding;
import com.zqcy.workbench.utils.Constants;
import com.zqcy.workbench.utils.MyTextWatcher;
import com.onemdos.storage.kv.MDOSPreferencesManager;

public class LoginActivity extends AppCompatActivity {

    private ActivityLoginBinding mLoginBinding;
    private int mLoginType = 1;  //1：验证码  2：账号密码
    private boolean agree = true;

    public static void startActivity(Context context) {
        Intent intent = new Intent(context, LoginActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mLoginBinding = ActivityLoginBinding.inflate(LayoutInflater.from(this));
        setContentView(mLoginBinding.getRoot());
        //设置全屏模式
        ColorUtils.setTransparent(this, Constants.isDarkModel, 1);
        initView();
    }

    private void initView() {
        mLoginBinding.edtMobile.addTextChangedListener(new MyTextWatcher() {
            @Override
            public void afterTextChanged(Editable editable) {
                if (editable.length() > 10) {
                    mLoginBinding.tvLogin.setBackground(ContextCompat.getDrawable(LoginActivity.this, R.drawable.bg_main_btn));
                    mLoginBinding.tvLogin.setEnabled(true);
                } else {
                    mLoginBinding.tvLogin.setBackground(ContextCompat.getDrawable(LoginActivity.this, R.drawable.bg_main_btn_disable));
                    mLoginBinding.tvLogin.setEnabled(false);
                }
            }
        });

        mLoginBinding.edtAccount.addTextChangedListener(new MyTextWatcher() {
            @Override
            public void afterTextChanged(Editable editable) {
                if (editable.length() > 0 && mLoginBinding.edtPassword.getText().length() > 0) {
                    mLoginBinding.tvLogin.setBackground(ContextCompat.getDrawable(LoginActivity.this, R.drawable.bg_main_btn));
                    mLoginBinding.tvLogin.setEnabled(true);
                } else {
                    mLoginBinding.tvLogin.setBackground(ContextCompat.getDrawable(LoginActivity.this, R.drawable.bg_main_btn_disable));
                    mLoginBinding.tvLogin.setEnabled(false);
                }
            }
        });

        mLoginBinding.edtPassword.addTextChangedListener(new MyTextWatcher() {
            @Override
            public void afterTextChanged(Editable editable) {
                if (editable.length() > 0 && mLoginBinding.edtAccount.getText().length() > 0) {
                    mLoginBinding.tvLogin.setBackground(ContextCompat.getDrawable(LoginActivity.this, R.drawable.bg_main_btn));
                    mLoginBinding.tvLogin.setEnabled(true);
                } else {
                    mLoginBinding.tvLogin.setBackground(ContextCompat.getDrawable(LoginActivity.this, R.drawable.bg_main_btn_disable));
                    mLoginBinding.tvLogin.setEnabled(false);
                }
            }
        });

        mLoginBinding.tvSwitchLoginType.setOnClickListener(view -> {
            if (mLoginType == 1) {
                mLoginType = 2;
            } else {
                mLoginType = 1;
            }
            bindLoginType();
        });

        mLoginBinding.llPrivacy.setOnClickListener(view -> {
            agree = !agree;
            if (agree) {
                mLoginBinding.ivAgree.setImageResource(R.mipmap.ic_circle_selected);
            } else {
                mLoginBinding.ivAgree.setImageResource(R.mipmap.ic_circle_unselected);
            }
        });

        mLoginBinding.tvLogin.setOnClickListener(view -> {
            if (!agree) {
                Toast.makeText(LoginActivity.this, "请先同意隐私协议", Toast.LENGTH_SHORT).show();
                return;
            }
            ProgressDialog pd = ProgressDialog.show(this, "", "正在加载中", true);
            if (mLoginType == 1) {
                String phone = mLoginBinding.edtMobile.getText().toString().trim();
                if (TextUtils.isEmpty(phone)) {
                    Toast.makeText(LoginActivity.this, R.string.phone_not_empty, Toast.LENGTH_SHORT).show();
                    return;
                }

                EpassAuthSDK.Builder().sendSms(LoginActivity.this, phone, new SendSmsCallBack() {
                    @Override
                    public void getSmsSuccess(boolean isSuccess) {
                        pd.dismiss();
                        if (isSuccess) {
                            CodeLoginActivity.startActivity(LoginActivity.this, phone);
                        } else {
                            Toast.makeText(LoginActivity.this, "密码单次认证失败", Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void error(String code, String message) {
                        pd.dismiss();
                        Toast.makeText(LoginActivity.this, getString(R.string.account_password_error) + " :" + message, Toast.LENGTH_SHORT).show();
                    }
                });
            } else {
                String account = mLoginBinding.edtAccount.getText().toString().trim();
                String password = mLoginBinding.edtPassword.getText().toString().trim();
                if (TextUtils.isEmpty(account) || TextUtils.isEmpty(password)) {
                    Toast.makeText(LoginActivity.this, R.string.account_password_not_empty, Toast.LENGTH_SHORT).show();
                    return;
                }
                EpassAuthSDK.Builder().linkPasswordAuth(LoginActivity.this, account, password, new AuthCallBack() {
                    @Override
                    public void doAuthSuccess(String authList, String authedList) {
                        Log.d("tag", "@@@@ doAuthSuccess authList:" + authList + " authedList:" + authedList);
                        pd.dismiss();
                        MDOSPreferencesManager.getInstance(LoginActivity.this, Constants.APP_SP_NAME).putBoolean(Constants.IS_LOGIN, true).apply();
                        MainActivity.startActivity(LoginActivity.this);
                        if (!isDestroyed()) {
                            finish();
                        }
                    }

                    @Override
                    public void needBindDevice(String authList) {
                        Log.d("tag", "@@@@ needBindDevice authList:" + authList);
                    }

                    @Override
                    public void error(String code, String message) {
                        Log.d("tag", "@@@@ error code:" + code + " message:" + message);
                        pd.dismiss();
                        Toast.makeText(LoginActivity.this, getString(R.string.account_password_error) + " :" + message, Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });
    }

    private void bindLoginType() {

        mLoginBinding.edtMobile.setText("");
        mLoginBinding.edtAccount.setText("");
        mLoginBinding.edtPassword.setText("");

        if (mLoginType == 1) {
            mLoginBinding.llLoginPhoneInput.setVisibility(View.VISIBLE);
            mLoginBinding.llLoginPasswordInput.setVisibility(View.GONE);

            mLoginBinding.tvLogin.setText(getString(R.string.get_phone_code));
            mLoginBinding.tvSwitchLoginType.setText(getString(R.string.account_login));
        } else {
            mLoginBinding.llLoginPhoneInput.setVisibility(View.GONE);
            mLoginBinding.llLoginPasswordInput.setVisibility(View.VISIBLE);

            mLoginBinding.tvLogin.setText(getString(R.string.login));
            mLoginBinding.tvSwitchLoginType.setText(getString(R.string.code_login));
        }
    }
}